package com.controller;

import com.pojo.User;
import com.service.TestService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Rxiao
 * @Date 2018/08/26/0026 10:23
 * @Version: 1.0
 */
@Controller
public class TestController {
    @Resource
    private TestService testService;

    @RequestMapping(value = "/{id}/{name}")
    @ResponseBody
    public User test(@PathVariable String id, @PathVariable String name) {
        return testService.findUser(id);
    }

    @RequestMapping("/log")
    public String log() {
        return "aaa";
    }

    @RequestMapping(value = "/file", method = RequestMethod.POST)
    @ResponseBody
    public String file(MultipartFile file) {
        if (file.isEmpty()) {
            return "空的";
        }
        String filename = file.getOriginalFilename();
        String type = file.getContentType();
        String name = file.getName();
        try {
            file.transferTo(new File("E:\\" + filename));
        } catch (IOException e) {
            e.printStackTrace();
            return "失败";
        }
        return "成功";
    }

    @RequestMapping(value = "/file1", method = RequestMethod.POST)
    @ResponseBody
    public String file1(MultipartHttpServletRequest request) {
        List<MultipartFile> files = request.getFiles("files");
        List<String> collect = files.parallelStream().filter(x -> !x.isEmpty()).map(x -> {
            String filename = x.getOriginalFilename();
            String type = x.getContentType();
            String name = x.getName();
            try {
                x.transferTo(new File("E:\\" + filename));
                return "成功";
            } catch (IOException e) {
                e.printStackTrace();
                return "失败";
            }
        }).collect(Collectors.toList());
        if (collect.contains("失败")) {
            return "失败";
        }
        return "成功";
    }

    /**
     * 机试用的
     *
     * @ModelAttribute注解可以自动将数据放到model中在前台取
     */
    @GetMapping("/findAll")
    public String findAll(String FName, @ModelAttribute("ids") String ids, Model model) {
        model.addAttribute("list", testService.findAll(FName));
        return "info";
    }

    @GetMapping("/toEdit/{id}")
    public String toEdit(@PathVariable int id, Model model) {
        model.addAttribute("l", testService.findByID(id));
        model.addAttribute("list", testService.findNianji());
        return "edit";
    }

    @PostMapping("edit")
    public String edit(@RequestParam Map map) {
        try {
            testService.edit(map);
        } catch (Exception e) {
        }
        return "redirect:/findAll";
    }

    @GetMapping("/del/{id}")
    public String del(@PathVariable int id) {
        try {
            testService.delByID(id);
        } catch (Exception e) {
        }
        return "redirect:/findAll";
    }

    @GetMapping("/toAdd")
    public String toAdd(Model model) {
        model.addAttribute("list", testService.findNianji());
        return "add";
    }

    @PostMapping("/add")
    public String add(@RequestParam Map map, String ids, RedirectAttributes redirectAttributes) {
        try {
            testService.add(map);
            redirectAttributes.addFlashAttribute("ids", ids);
        } catch (Exception e) {
        }
        return "redirect:/findAll";
    }

    /**
     * mybatis级联查询
     */
    @GetMapping("/findJson")
    @ResponseBody
    public List<Map> findJson() {
        return testService.findJson();
    }
}